package com.ryg.chapter_3;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class TestActivity extends Activity implements OnClickListener,
        OnLongClickListener {

    private static final String TAG = "TestActivity";

    private static final int MESSAGE_SCROLL_TO = 1;
    private static final int FRAME_COUNT = 30;
    private static final int DELAYED_TIME = 33;

    private Button mButton1;
    //    private View mButton2;
    private Button mButton2;

    private int mCount = 0;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_SCROLL_TO: {
                    mCount++;
                    if (mCount <= FRAME_COUNT) {
                        float fraction = mCount / (float) FRAME_COUNT;
                        int scrollX = (int) (fraction * 100);
                        mButton1.scrollTo(scrollX, 0);
                        mHandler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO, DELAYED_TIME);
                    }
                    break;
                }

                default:
                    break;
            }
        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initView();
    }

    private void initView() {
        mButton1 = (Button) findViewById(R.id.button1);
        mButton1.setOnClickListener(this);
        mButton2 = (Button) findViewById(R.id.button2);
        mButton2.setOnLongClickListener(this);
        mButton2.setOnClickListener(this);

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            Log.d(TAG, "button1.left=" + mButton1.getLeft());
            Log.d(TAG, "button1.x=" + mButton1.getX());
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mButton1) {

            int lLeft = mButton1.getLeft();
            int lTop = mButton1.getTop();
            float lx = mButton1.getX();
            float ly = mButton1.getY();
            float lx1 = mButton1.getTranslationX();
            float ly1 = mButton1.getTranslationY();
            Log.d(TAG, "--1--button1.left=" + mButton1.getLeft());
            Log.d(TAG, "--1--button1.x=" + mButton1.getX());
//            mButton1.setTranslationX(100);
//
//            ObjectAnimator.ofFloat(mButton1, "translationX", 0, 100)
//                    .setDuration(1000).start();
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mButton1
                    .getLayoutParams();
            params.width += 100;
            params.leftMargin += 100;
            mButton1.requestLayout();
            mButton1.setLayoutParams(params);
//            Log.d(TAG, "--2--button1.left=" + mButton1.getLeft());
//            Log.d(TAG, "--2--button1.x=" + mButton1.getX());


//            final int startX = 0;
//            final int deltaX = 100;
//            ValueAnimator animator = ValueAnimator.ofInt(0,
//                    1).setDuration(1000);
//            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                @Override
//                public void onAnimationUpdate(ValueAnimator animator) {
//                    float fraction = animator.getAnimatedFraction();
//                    mButton1.scrollTo(startX + (int) (deltaX * fraction), 0);
//                }
//            });
//            animator.start();


//            mHandler.sendEmptyMessageDelayed(MESSAGE_SCROLL_TO, DELAYED_TIME);
        } else if (v == mButton2) {
//            mButton1.requestLayout();
            Log.d(TAG, "--3--button1.left=" + mButton1.getLeft());
            Log.d(TAG, "--3--button1.x=" + mButton1.getX());
            Log.d(TAG, "--2--button1.setTranslationX=" + mButton1.getTranslationX());

        }
    }


    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(this, "long click", Toast.LENGTH_SHORT).show();

        return true;
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume");
        super.onResume();
    }
}
